package com.yuanfengerp.basicInfoManage.storeinfo.service.impl;

import com.yuanfengerp.basicInfoManage.storeinfo.dao.IStoreinfoDao;
import com.yuanfengerp.basicInfoManage.storeinfo.model.Storeinfo;
import com.yuanfengerp.basicInfoManage.storeinfo.service.IStoreinfoService;
import com.yuanfengerp.publicUtil.Result;
import com.yuanfengerp.publicUtil.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class StoreinfoServiceImpl implements IStoreinfoService {
    @Autowired
    IStoreinfoDao srfa;

    @Override
    public Result<List<Storeinfo>> selectStoreAll(Map<String, Object> params) {
        params.put("pagenum",((int) params.get("pagenum")-1) * (int) params.get("num"));
        List<Storeinfo> storeinfos = srfa.selectStoreAll(params);
        if (!(storeinfos.isEmpty()))
            return Result.success(storeinfos, srfa.Count());
        return Result.failure(400,"没有这条信息");
    }

    @Override
    public Result<String> addStore(Storeinfo storeinfo) {
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0);
        storeinfo.setStore_id(idWorker.nextId());
        storeinfo.setStore_time(new java.sql.Date(System.currentTimeMillis()));
        int i = srfa.addStore(storeinfo);
        if (i > 0)
            return Result.success("添加成功");
        return Result.failure(400,"添加失败");
    }

    @Override
    public Result<String> updateStore(Storeinfo storeinfo) {
        int i = srfa.updateStore(storeinfo);
        if (i > 0)
            return Result.success("修改成功");
        return Result.failure(400,"修改失败");
    }

    @Override
    public Result<Storeinfo> selectStore(String id) {
        Storeinfo storeinfo = srfa.selectStore(id);
        if (storeinfo != null)
            return Result.success(storeinfo);
        return Result.failure(400,"没找到");
    }
}
